package ngames.com.sushitv.Fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

import ngames.com.sushitv.AppApplication;
import ngames.com.sushitv.R;
import ngames.com.sushitv.action.SearchRequestAction;
import ngames.com.sushitv.temp.Adapter.MyPagerAdapter;
import ngames.com.sushitv.view.ClearEditText;
import ngames.com.sushitv.view.Indicator;

/**
 * A simple {@link Fragment} subclass.
 */
public class SearchResultFragment extends Fragment implements View.OnClickListener {
    private ClearEditText clearEditText;
    private View view;
    private Indicator indicator;
    private ViewPager viewPager;
    private ArrayList<Fragment> fragments;
    private MyPagerAdapter adapter;
    private Fragment allFragment, animeFragment, fanMadeFragment, userFragment;
    private TextView releaseTimeTxt, viewsTxt, commentsTxt, danmakuTxt, favoritesTxt;
    private View screenlayout;
    private LinearLayout screenBtn;
    private Boolean screenShow = false;
    private int nowPage = 0;
    private SearchRequestAction searchRequestAction;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_search_result, container, false);
        initView();
        return view;
    }

    private void initView() {
        screenlayout = view.findViewById(R.id.layout_screen);
        screenlayout.setVisibility(View.GONE);
        screenBtn = (LinearLayout) view.findViewById(R.id.linear_screen_btn);
        screenBtn.setOnClickListener(this);
        releaseTimeTxt = (TextView) view.findViewById(R.id.text_screen_releaseTime);
        viewsTxt = (TextView) view.findViewById(R.id.text_screen_Views);
        commentsTxt = (TextView) view.findViewById(R.id.text_screen_comments);
        danmakuTxt = (TextView) view.findViewById(R.id.text_screen_danmaku);
        favoritesTxt = (TextView) view.findViewById(R.id.text_screen_favorites);
        releaseTimeTxt.setOnClickListener(this);
        viewsTxt.setOnClickListener(this);
        commentsTxt.setOnClickListener(this);
        danmakuTxt.setOnClickListener(this);
        favoritesTxt.setOnClickListener(this);
        indicator = (Indicator) view.findViewById(R.id.indicator_search_result);
        viewPager = (ViewPager) view.findViewById(R.id.viewPager_search_result);
        LayoutInflater inflater = LayoutInflater.from(getActivity());
        View Pview = AppApplication.getSearchFragmentView();

        clearEditText = (ClearEditText) Pview.findViewById(R.id.clearEdit_search);
        //clearEditText.setText(AppApplication.getSearchKeyWord());
        //clearEditText.setEnabled(false);
        clearEditText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                clearEditText.setText("");
                AppApplication.getChangFragment().ChangeFragment(1);
            }
        });
        //clearEditText.clearFocus();
        //clearEditText.setFocusable(false);
       /* LayoutInflater mInflater=LayoutInflater.from(getContext());
        for(int i=0;i<4;i++) {
            final TextView tv = (TextView) mInflater.inflate(R.layout.view_textview_search, indicator, false);
            tv.setText(i+"");
            indicator.addView(tv);
        }*/
        fragments = new ArrayList<>();
        allFragment = new SRAllFragment();
        animeFragment = new SRAnimeFragment();
        fanMadeFragment = new SRFanMadeFragment();
        userFragment = new SRUserFragment();
        fragments.add(allFragment);
        fragments.add(animeFragment);
        fragments.add(fanMadeFragment);
        fragments.add(userFragment);
        adapter = new MyPagerAdapter(getChildFragmentManager(), fragments);
        viewPager.setAdapter(adapter);
        indicator.setViewPager(viewPager, 0);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                nowPage = position;
                screenShow = false;
                screenlayout.setVisibility(View.GONE);
                if (position == 3) {
                    screenBtn.setEnabled(false);
                } else {
                    screenBtn.setEnabled(true);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public void onClick(View view) {
        initTextVew();
        switch (view.getId()) {
            case R.id.text_screen_releaseTime:
                releaseTimeTxt.setBackgroundResource(R.drawable.screen_background);
                newData(0);
                break;
            case R.id.text_screen_Views:
                viewsTxt.setBackgroundResource(R.drawable.screen_background);
                newData(1);
                break;
            case R.id.text_screen_comments:
                commentsTxt.setBackgroundResource(R.drawable.screen_background);
                newData(2);
                break;
            case R.id.text_screen_danmaku:
                danmakuTxt.setBackgroundResource(R.drawable.screen_background);
                newData(3);
                break;
            case R.id.text_screen_favorites:
                favoritesTxt.setBackgroundResource(R.drawable.screen_background);
                newData(4);
                break;
            case R.id.linear_screen_btn:
                if (screenShow) {
                    screenlayout.setVisibility(View.GONE);
                    screenShow = false;
                } else {
                    screenlayout.setVisibility(View.VISIBLE);
                    screenShow = true;
                }
                break;
        }
    }

    private void newData(int order) {
        searchRequestAction = new SearchRequestAction(getActivity(), view, 1, "all", AppApplication.getSearchKeyWord(), order + "");
        searchRequestAction.onCreate();
        if (nowPage == 2) {
            searchRequestAction = new SearchRequestAction(getActivity(), view, 1, "anime", AppApplication.getSearchKeyWord(), "");
            searchRequestAction.onCreate();
        }
        if (nowPage == 3) {
            searchRequestAction = new SearchRequestAction(getActivity(), view, 1, "fan_made", AppApplication.getSearchKeyWord(), "");
            searchRequestAction.onCreate();
        }
    }

    private void initTextVew() {
       /* releaseTimeTxt.setBackgroundResource(R.drawable.abc_btn_rating_star_off_mtrl_alpha);
        viewsTxt.setBackground(null);
        commentsTxt.setBackground(null);
        danmakuTxt.setBackground(null);
        favoritesTxt.setBackground(null);*/
        releaseTimeTxt.setBackgroundColor(Color.parseColor("#372c3b"));
        viewsTxt.setBackgroundColor(Color.parseColor("#372c3b"));
        commentsTxt.setBackgroundColor(Color.parseColor("#372c3b"));
        danmakuTxt.setBackgroundColor(Color.parseColor("#372c3b"));
        favoritesTxt.setBackgroundColor(Color.parseColor("#372c3b"));
    }
}
